<?php

/*
 *
 * @file name: mode-tudou.php
 *
 * @description: Get the video sources on tudou.com
 * @author: RayLee[itaoyuan.org]
 * @version: 1.1.6
 * @license: GNU/GPL
 * @copyright: RayLee[RayLee@itaoyuan.org]
 *
 */

//--------------------------------------------------------------------------------------------//
 
// Include the function package
include_once(dirname(__FILE__)."/functions.php");

//--------------------------------------------------------------------------------------------//

/*
 * @function name: tudou_change_host
 * @description: CDN crack
 *
 * @parameter $url(string): required, the old url
 *
 * @return: string, the new url
 */

function tudou_change_host($url){
	// tudou.com cdn host list
	$host_list = array(
		'58.221.54.7',		'61.153.87.201',	'113.17.169.2',		'113.17.140.3',
		'113.17.140.5',		'113.17.140.7',		'113.17.169.6',		'113.17.169.7',
		'113.17.169.8',		'113.17.169.10',	'113.17.169.11',	'113.17.169.13',
		'113.17.140.9',		'113.17.140.11',	'114.80.236.193',	'114.80.236.195',
		'114.80.236.200',	'114.80.236.202',	'114.80.236.204',	'114.80.236.208',
		'115.238.133.2',	'115.238.133.9',	'115.238.134.37',	'117.21.186.3',
		'117.21.186.9',		'119.84.65.9',		'119.84.65.14',		'119.84.65.19',
		'119.84.31.45',		'119.84.31.129',	'119.84.31.131',	'119.84.31.132',
		'119.84.31.133',	'119.84.31.134',	'119.84.31.135',	'119.84.31.136',
		'119.84.31.138',	'119.84.31.139',	'119.147.173.2',	'180.137.254.2',
		'180.137.254.3',	'180.137.254.6',	'180.137.254.7',	'180.137.254.9',
		'180.137.254.11',	'180.153.94.129',	'180.153.94.131',	'180.153.94.132',
		'180.153.94.133',	'180.153.94.134',	'180.153.94.135',	'180.153.94.136',
		'180.153.94.137',	'180.153.94.138',	'180.153.94.161',	'180.153.94.164',
		'180.153.94.166',	'180.153.94.169',	'180.153.94.170',	'180.153.94.196',
		'180.153.94.198',	'180.153.94.202',	'180.153.94.204',	'183.60.102.12',
		'218.76.201.4',		'218.76.201.5',		'221.204.192.140',	'221.204.192.142',
		'221.204.192.147',	'221.204.192.148',	'221.204.192.205',	'221.204.192.207',
		'221.204.192.208',
		
	);
	
	$host = parse_url($url);
	$referer = 'http://www.tudou.com';
	$i = 0;
	
	while($i < count($host_list)){
		$rurl = str_ireplace($host['host'], $host_list[$i], $url);
		$header = get_content($rurl,true,true,$referer);
		
		if(stristr($header,"200 OK")){
			$curl = $rurl;
			break;
		}
		
		$i++;
	}
	
	if(!empty($curl))
		return $curl;
	else
		return $url;
}

//--------------------------------------------------------------------------------------------//

/*
 *
 * @function name: decode_tudou
 * @description: decode the url(s) of the video on tudou.com
 *
 * @parameter $iid(string): required, the video ID you get from tudou.com
 * @parameter $display(bool): optional, display the url(s) or not. default true.
 *
 * @return: array, the video info[url, size, duration]
 *
 */

function decode_tudou($iid,$display = true){
	// Base url.
	$url = 'http://v2.tudou.com/v2/cdn?id='.$iid;
	
	// custom referer
	$referer = 'http://www.tudou.com';
	
	// get remote page contents
	$content = get_content($url,false,false,$referer);
	
	// Get video url(s)
	preg_match_all('~>(http[s]{0,1}://.*)<~iUs',$content,$vurl);
	
	// No result.
	if(count($vurl[1]) == 0){
		if($display) 
			echo '<p class="info">无法获取到您指定的视频资源，资源已被删除或者您提供的信息有误。</p>';
		else
			return NULL;
	}
	// if the server returned one or more resource(s),
	// continue to get more information
	else{
		// Get video duration
		preg_match('~time="(\d+)"~iUs',$content,$time);
		
		// Get video size
		preg_match_all('~size="(\d+)"~iUs',$content,$size);
		
		// display
		if($display)
			echo '<a href="'.tudou_change_host($vurl[1][count($vurl[1])-1]).'" target="_blank">视频地址</a>';
		// return
		else{
			$video['duration'][0] = round($time[1] / 1000,3);
			$video['size'][0] = $size[1][count($vurl[1])-1];
			$video['url'][0] = tudou_change_host($vurl[1][count($vurl[1])-1]);
			
			return $video;
		}
	}
}
?>